﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System;

namespace Ascend
{
	/// <summary>
	/// A set of angles that describes the rotation of an object in three dimensions.
	/// </summary>
	public struct Angle3D : IEquatable<Angle3D>
	{
		#region Lifecycle
		/// <summary>
		/// Initializes a new instance of Ascend.Angle3D structure with given angles.
		/// </summary>
		/// <param name="x">
		/// The X-axis angle (roll) in degrees.
		/// </param>
		/// <param name="y">
		/// The Y-axis angle (yaw) in degrees.
		/// </param>
		/// <param name="z">
		/// The Z-axis angle (pitch) in degrees.
		/// </param>
		/// <remarks>
		/// The rotation convention used by Ascend is yaw-pitch-roll / YZX.
		/// </remarks>
		public Angle3D(double x, double y, double z)
		{
			_x = x;
			_y = y;
			_z = z;
		}
		#endregion

		#region Public Properties
		private double _x;
		/// <summary>
		/// Gets this Ascend.Angle3D's X-axis angle (roll) in degrees.
		/// </summary>
		/// <remarks>
		/// The rotation convention used by Ascend is yaw-pitch-roll / YZX.
		/// </remarks>
		public double X
		{
			get
			{
				return _x;
			}
		}

		private double _y;
		/// <summary>
		/// Gets this Ascend.Angle3D's Y-axis angle (yaw) in degrees.
		/// </summary>
		/// <remarks>
		/// The rotation convention used by Ascend is yaw-pitch-roll / YZX.
		/// </remarks>
		public double Y
		{
			get
			{
				return _y;
			}
		}

		private double _z;
		/// <summary>
		/// Gets this Ascend.Angle3D's Z-axis angle (pitch) in degrees.
		/// </summary>
		/// <remarks>
		/// The rotation convention used by Ascend is yaw-pitch-roll / YZX.
		/// </remarks>
		public double Z
		{
			get
			{
				return _z;
			}
		}
		#endregion

		#region Operators
		/// <summary>
		/// Compares two Ascend.Angle3D structures for equality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the X, Y, and Z angles of <paramref name="value1"/> are equal to those
		/// of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		/// <remarks>
		/// Because double values can lose precision when arithmetic operations are performed on
		/// them, a comparison between two Ascend.Angle3D instances that are logically equal might
		/// fail.
		/// </remarks>
		public static bool operator ==(Angle3D value1, Angle3D value2)
		{
			return value1.Equals(value2);
		}

		/// <summary>
		/// Compares two Ascend.Angle3D structures for inequality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the X, Y, and Z angles of <paramref name="value1"/> are different from
		/// those of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		/// <remarks>
		/// Because double values can lose precision when arithmetic operations are performed on
		/// them, a comparison between two Ascend.Angle3D instances that are logically equal might
		/// fail.
		/// </remarks>
		public static bool operator !=(Angle3D value1, Angle3D value2)
		{
			return !value1.Equals(value2);
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Compares two Ascend.Angle3D structures for equality.
		/// </summary>
		/// <param name="value">
		/// The Ascend.Angle3D instance to compare to this instance.
		/// </param>
		/// <returns>
		/// Returns true if the instances are equal. Returns false otherwise.
		/// </returns>
		/// <remarks>
		/// Because double values can lose precision when arithmetic operations are performed on
		/// them, a comparison between two Ascend.Angle3D instances that are logically equal might
		/// fail.
		/// </remarks>
		public bool Equals(Angle3D value)
		{
			return X == value.X && Y == value.Y && Z == value.Z;
		}

		/// <summary>
		/// Determines whether the specified object is an Ascend.Angle3D structure and if so,
		/// whether the X, Y, and Z angles of the specified object are equal to those of this
		/// Ascend.Angle3D instance.
		/// </summary>
		/// <param name="obj">
		/// The object to compare.
		/// </param>
		/// <returns>
		/// Returns true if the specified object is an Ascend.Angle3D structure and is identical to
		/// this instance. Returns false otherwise.
		/// </returns>
		/// <remarks>
		/// Because double values can lose precision when arithmetic operations are performed on
		/// them, a comparison between two Ascend.Angle3D instances that are logically equal might
		/// fail.
		/// </remarks>
		public override bool Equals(object obj)
		{
			if (obj == null || GetType() != obj.GetType())
			{
				return false;
			}

			return Equals((Angle3D)obj);
		}

		/// <summary>
		/// Returns a hash code for this Ascend.Angle3D structure.
		/// </summary>
		/// <returns>
		/// Returns a hash code for this Ascend.Angle3D structure.
		/// </returns>
		public override int GetHashCode()
		{
			return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
		}

		/// <summary>
		/// Creates a string representation of this Ascend.Angle3D structure.
		/// </summary>
		/// <returns>
		/// Returns a string containing the X, Y, and Z angles of this Ascend.Angle3D structure.
		/// </returns>
		public override string ToString()
		{
			return X + ", " + Y + ", " + Z;
		}
		#endregion

		#region Static Methods
		/// <summary>
		/// Compares two Ascend.Angle3D structures for equality.
		/// </summary>
		/// <param name="value1">
		/// The first value to compare.
		/// </param>
		/// <param name="value2">
		/// The second value to compare.
		/// </param>
		/// <returns>
		/// Returns true if the X, Y, and Z angles of <paramref name="value1"/> are equal to those
		/// of <paramref name="value2"/>. Returns false otherwise.
		/// </returns>
		/// <remarks>
		/// Because double values can lose precision when arithmetic operations are performed on
		/// them, a comparison between two Ascend.Angle3D instances that are logically equal might
		/// fail.
		/// </remarks>
		public static bool Equals(Angle3D value1, Angle3D value2)
		{
			return value1.Equals(value2);
		}
		#endregion
	}
}
